


from dxlql import get_all_school_detail as dxlql_get_all_school_detail
from dxlql import get_zyz_detail as dxlql_get_zyz_detail
from datetime import datetime
from DBService import MysqlService
from easyspider.pipelines.commonMysqlpipeline import process_item
from loguru import logger

class Base(object):
    custom_settings = {
        "MYSQL_HOST": "124.221.151.161",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "molbase1010",
        "MYSQL_PORT": 10006,
        "MYSQL_DB": "zy",
        "MYSQL_TABLE": "学校和专业组",
        "CONCURRENT_REQUESTS": 2,
        "DOWNLOAD_DELAY": 4
    }

    def __init__(self):
        
        self.mysql_server = MysqlService(
            self.custom_settings["MYSQL_HOST"],
            self.custom_settings["MYSQL_USER"],
            self.custom_settings["MYSQL_PASSWORD"],
            self.custom_settings["MYSQL_PORT"]
        )
        self.mysql_server.select_db(self.custom_settings["MYSQL_DB"])


class ZY_dxlql(Base):
    def __init__(self):
        super().__init__()
    
    def get_all_school(self):
        result = dxlql_get_all_school_detail()
        # detail_result = []
        for page in result:
            for row in page["data"]["rows"]:
                if row["sf985"]:
                    school_type = "985"
                elif row["sf211"]:
                    school_type = "211"
                elif row["sfsyl"]:
                    school_type = "双一流"
                else:
                    school_type = "双非"
                base_line = {
                    "学校": row["yxmc"],
                    "自定义序号": None,
                    "page_idx": page["page_idx"],                    
                    "办学类型": None,
                    "临时备注": None,
                    "学校类型": school_type,
                    "代码": row["sb"],
                    "地区": row["szdqmc"],
                }
                for zy in row["zys"]:
                    line = base_line.copy()
                    if "中外" in zy["zymc"]:
                        line["办学类型"] = "中外合作"
                    else:
                        line["办学类型"] = "正常"
                    zyid = zy["zyzid"]
                    
                    year_2023_min_score, year_2023_max_index, year_min_score, year_max_index, cur_jsh, year_2023_jhs, diff_jhs = self.get_from_subs(zy["subs"])
                    contain_zy = []
                    for _zy in zy["bhzy2"].split(";"):
                        if "-" in _zy:
                            contain_zy.append(_zy.split("-")[1])
                    contain_zy = ";".join(contain_zy)
                    if not cur_jsh or not year_2023_jhs:
                        diff_jsh = None
                    else:
                        diff_jsh = cur_jsh - year_2023_jhs
                    line.update({
                        "专业组": zy["zydm"],
                        "选科要求": zy["zxkmyq"],
                        "23年最低等效分": year_2023_min_score,
                        "23年最大位次": year_2023_max_index,
                        "历年最低等效分": year_min_score,
                        "历年最大位次": year_max_index,
                        "去年招生人数": year_2023_jhs,
                        "今年招生人数": cur_jsh,
                        "浮动人数": diff_jhs,
                        "包含专业": contain_zy,
                        "专业组ID": zyid,
                    })
                    line.update({
                        "easyspider": {
                            "save_mysql": True,
                            "mysql_config": {
                                "db": self.custom_settings["MYSQL_DB"],
                                "table": self.custom_settings["MYSQL_TABLE"],
                                "hash_check_enable": False,
                                "hash_check_item": ["学校", "专业组ID"]
                            }
                        }
                    })
                    process_item(self.mysql_server, line)


    def get_from_subs(self, subs):
        year_2023_min_score = float('inf')
        year_2023_max_index = float('-inf')
        
        year_min_score = float('inf')
        year_max_index = float('-inf')
        
        cur_jsh = 0
        year_2023_jhs = 0
        for line in subs:
            cur_index = line["rdwc"] or float('-inf')
            cur_score = line["zdtwf"] or float('inf')
            year_min_score = min(year_min_score, cur_score)
            year_max_index = max(year_max_index, cur_index)
            if line["nd"] == (datetime.now().year - 1):
                year_2023_max_index = line["rdwc"]
                year_2023_min_score = line["zdtwf"]
                if line["jnjhrs"]:
                    cur_jsh = line["jnjhrs"]
                if line["jhs"]:
                    year_2023_jhs = line["jhs"]
        diff_jhs = cur_jsh - year_2023_jhs

        if year_2023_min_score == float('inf'):
            year_2023_min_score = -1
        if year_2023_max_index == float('-inf'):
            year_2023_max_index = -1
        if year_min_score == float('inf'):
            year_min_score = -1
        if year_max_index == float('-inf'):
            year_max_index = -1

        return year_2023_min_score, year_2023_max_index, year_min_score, year_max_index, cur_jsh, year_2023_jhs, diff_jhs
    

    def get_major_detail(self):

        major_id_list = self.mysql_server.query("select 学校,专业组,选科要求, 专业组ID from zy.学校和专业组")
        all_cnt = len(major_id_list)
        for idx, _id in enumerate(major_id_list):
            if idx < 2376:
                continue
            
            logger.info(f"[{idx} / {all_cnt}]")
            major_detail = dxlql_get_zyz_detail(_id["专业组ID"])
            new_rows = []
            for major in major_detail["data"]:
                year_2023_min_score, year_2023_max_index, year_min_score, year_max_index, cur_jsh, year_2023_jhs, diff_jhs = self.get_from_subs(major["subs"])
                cur_year = datetime.now().year - 1
                if major["sfbz"] == "NULL":
                    major["sfbz"] = None
                item = {
                    "学校": _id["学校"],
                    "专业组": _id["专业组"],
                    "专业组ID": _id["专业组ID"],
                    "选科要求": _id["选科要求"],
                    "专业名字": major["zymc"],
                    "学费": major["sfbz"],
                    f"{cur_year}年最低等效分数": year_2023_min_score,
                    f"{cur_year}年最高位次": year_2023_max_index,
                    "近三年最低等效分": year_min_score,
                    "近三年最高位次": year_max_index,
                    "今年招生计划": cur_jsh,
                    f"{cur_year}年招生计划": year_2023_jhs,
                    "招生计划差值": diff_jhs,
                }
                item.update({
                        "easyspider": {
                            "save_mysql": True,
                            "mysql_config": {
                                "db": self.custom_settings["MYSQL_DB"],
                                "table": "专业组",
                                "hash_check_enable": False,
                                "hash_check_item": ["学校", "专业组ID", "专业名字", "招生计划差值"]
                            }
                        }
                    })
                # print(item)
                process_item(self.mysql_server, item)
                

if __name__ == "__main__":
    # ZY_dxlql().get_all_school()
    ZY_dxlql().get_major_detail()